package tt.dz.order.vo;

import tt.dz.order.exception.BusinessException;
import tt.dz.order.pojo.OrderAlreadyPaid;
import tt.dz.order.util.page.DefaultPaginationBuilder;
import tt.dz.order.util.page.DefaultPaginationDirector;
import tt.dz.order.util.page.Pagination;
import tt.dz.order.util.page.PaginationDirector;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class OrderPayVo {
	private static final String select_all_sql = "select a.*, b.NAME as merchantCodeName ,pay.alreadyPaid from ( SELECT merchantCode, sum(price) as price  FROM order_already_paid  group by merchantCode) a left join ms_merchant b on a.merchantcode =b.merchant_code LEFT JOIN ( SELECT merchantcode, SUM (caculatedtotalprice) AS alreadyPaid FROM order_pay_info where status='7'  GROUP BY merchantcode) pay on pay.merchantcode=A.merchantcode where 1=1 ";
	// 运营商号
	private String merchantCode;
	private Date startDate;
	private Date endDate;



	/**
	 * 订单修改日志查询
	 * 
	 * @param queryVO
	 * @param curPageNo
	 * @param prePageNum
	 * @return
	 * @throws BusinessException
	 */
	public static Pagination<OrderAlreadyPaid> queryPagition(OrderPayVo queryVO, int curPageNo,
                                                             int prePageNum) throws BusinessException {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); 
		StringBuilder controlBuilder = new StringBuilder();

		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (queryVO.getMerchantCode()!= null) {
			controlBuilder.append("		AND a.merchantcode =:merchantCode");
			paramMap.put("merchantCode", queryVO.getMerchantCode());
		}
		String countSql = " select count(1) from ( SELECT merchantCode, sum(price) as price  FROM order_already_paid  group by merchantCode) a left join ms_merchant b on a.merchantcode =b.merchant_code LEFT JOIN ( SELECT merchantcode, SUM (caculatedtotalprice) AS alreadyPaid FROM order_pay_info where status='7' GROUP BY merchantcode) pay on pay.merchantcode=A.merchantcode where 1=1" + controlBuilder.toString();
		PaginationDirector director = new DefaultPaginationDirector(new DefaultPaginationBuilder(), countSql,
				select_all_sql + controlBuilder.append(" ORDER BY merchantCode DESC ").toString(), paramMap, curPageNo, prePageNum);
		Pagination<OrderAlreadyPaid> pagination = director.construct(OrderAlreadyPaid.class);

		return pagination;
	}


	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
}
